import Vue from 'vue'
import VueRouter from 'vue-router'
//引入Login组件
import Home from '../views/Home.vue'
import Index from '../views/Index.vue';
import axios from "axios"
Vue.prototype.$axios=axios
Vue.use(VueRouter)
/*解决当前导航点击两次回报错*/
const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => err)
}


const routes = [

  {
    path:"/login",
    name:'login',
    component: ()=>import('../views/Login'),
  },
  {
    path:"/",
    redirect:"/login"
  },
  {
    path:"/home",
    component: Home,
    redirect:"/index",
    children:[
      {
        path:"/index",
        component: Index,
      },
      {
        path:"/home/person",
        name:'person',
        component: ()=>import('../views/person/Person'),
      },
      {
        path:"/home/area",
        name:'area',
        component: ()=>import('../views/area/Area'),
      },
      {
        path:"/home/recordOfArea",
        name:'recordOfArea',
        component: ()=>import('../views/area/RecordOfArea'),
      },
      {
        path:"/home/AreaMonitor",
        name:'AreaMonitor',
        component: ()=>import('../views/area/AreaMonitor'),
      },
      {
        path:"/home/recordOfPerson",
        name:'recordOfPerson',
        component: ()=>import('../views/person/RecordOfPerson'),
      },
      {
        path:"/home/record",
        name:'record',
        component: ()=>import('../views/record/Record'),
      },
      {
        path:"/home/user",
        name:'user',
        component: ()=>import('../views/user/User'),
      },
    ]
  },

]


const router = new VueRouter({
  routes
})
router.beforeEach((to,from,next)=> {
  //to将要访问
  //from从哪访问
  //next接着干  重定向到url上next（）继续访问到tor路径
  if (to.path == '/login') return next();
  //获取user
  const userFlag = window.sessionStorage.getItem("user");//取出用户
  if (!userFlag) return next('/login');
  next();
})
export default router

